// 通过vue-router插件实现路由配置
import { createRouter, createWebHistory } from 'vue-router'
import type { App } from 'vue'

import type { RouteRecordRaw } from 'vue-router'
import { basicRoutes } from './routes/index'

export const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
  routes: basicRoutes as unknown as RouteRecordRaw[],
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 }),
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    from.path === '/login' ? next({ path: '/dashboard' }) : next()
  } else {
    next()
  }
})

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}
